import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/globalMethod/FFScript.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:mybase/base_new/help/JJShock.dart';
import 'package:mybase/base_new/privateWidget/JJClickContainer.dart';

// 点击事件
mixin JJClick {
  /// 点击事件名
  String? clickName;

  /// 点击事件
  Function? onClick;

  /// 长按事件
  Function()? onLongClick;

  /// 隐藏组件，但保留位置，默认值为false
  bool? hiddenKeepFrame;

  /// 隐藏组件，并清除位置，默认值为false,优先级低于hiddenKeepFrame
  bool? hiddenClearFrame;

  /// 重复点击最少间隔，毫秒，默认值400
  int? minClickIntervalMill;

  /// 触发类型
  /// 了解前提 [GestureDetector]嵌套时，[onTap] 不会穿透，但 [onTapDown] 会穿透。
  /// 当发生[嵌套]时，最底层使用保持默认，其他上层使用[upTrigger]
  /// [downTrigger] 使用 [onTapDown]。
  /// [upTrigger] 使用 [onTap]。
  ClickType? clickType = ClickType.downTrigger;

  /// 按下时的透明度
  double? clickedAlpha;

  /// 按下时的缩放
  double? clickedScale;

  /// 缩放的毫秒数
  int? scaleMill;

  /// 上一次点击时间
  int? _lastMill;

  /// 震动类型
  ShockType? shockType;

  /// 忽略本身的交互
  bool? ignoreInteraction;

  /// 忽略本身及子组件的交互
  bool? ignoreInteractionAll;

  final GlobalKey<JJClickContainerState> _getxKey = GlobalKey();

  Widget clickBuild(BuildContext context, Widget child) {
    return _inBuild(
      context,
      GestureDetector(
        behavior: HitTestBehavior.translucent,
        onLongPress: onLongClick,
        onTap: clickType == ClickType.upTrigger
            ? () async {
          if (_enableClickIf()) {
            if (onClick != null) {
              repeatedClickJudgment(() {
                onClick!();
              });

              onTapDown();
              await globalAwait(mill: 30);
              onTapUp();
            }
          }
        }
            : null,
        onTapDown: clickType == ClickType.upTrigger
            ? null
            : (e) {
          if (_enableClickIf()) {
            onTapDown(e: e);
          }
        },
        onTapCancel: clickType == ClickType.upTrigger
            ? null
            : () {
          if (_enableClickIf()) {
            onTapCancel();
          }
        },
        onTapUp: clickType == ClickType.upTrigger
            ? null
            : (e) async {
          if (_enableClickIf()) {
            onTapUp(e: e);
            if (onClick != null) {
              repeatedClickJudgment(() {
                onClick!();
              });
            }
          }
        },
        child: JJClickContainer(
          key: _getxKey,
          enableClick: onClick != null || onLongClick != null,
          clickName: clickName,
          clickedAlpha: clickedAlpha,
          clickedScale: clickedScale,
          scaleMill: scaleMill,
          child: child,
        ),
      ),
    );
  }

  Widget _inBuild(BuildContext context, Widget child) {
    // 忽略交互
    if (ignoreInteraction != true) {
      ignoreInteraction = (onClick == null && onLongClick == null);
    }

    Widget view = child;
    if (hiddenKeepFrame == true) {
      view = Opacity(
        opacity: hiddenKeepFrame == true ? 0 : 1,
        child: child,
      );
    } else if (hiddenClearFrame == true) {
      view = Offstage(
        offstage: hiddenClearFrame ?? false,
        child: child,
      );
    }

    if (ignoreInteraction == true) {
      view = IgnorePointer(
        ignoring: true, // 设置为 true 则禁用交互
        child: view,
      );
    } else if (ignoreInteractionAll == true) {
      view = AbsorbPointer(
        absorbing: true, // 设置为 true 则禁用交互
        child: view,
      );
    }

    return view;
  }

  // 点击按下
  onTapDown({TapDownDetails? e}) {
    _getxKey.currentState?.setTapDown(true);
  }

  // 点击抬起
  onTapUp({TapUpDetails? e}) async {
    if (shockType != null) {
      JJShock.shock(shockType!);
    }
    await globalAwait(mill: 150);
    _getxKey.currentState?.setTapDown(false);
  }

  // 点击取消
  onTapCancel() async {
    _getxKey.currentState?.setTapDown(false);
  }

  // 是否可能能点击
  bool _enableClickIf() {
    return (onClick != null || onLongClick != null) && hiddenKeepFrame != true && hiddenClearFrame != true;
  }

  /// 重复点击判断
  repeatedClickJudgment(Function? fun) {
    if (fun != null) {
      if (isBeyondClickInterval()) {
        fun();
      }
    }
  }

  /// 判断是否超过点击间隔
  bool isBeyondClickInterval() {
    int currentMill = DateTime
        .now()
        .millisecondsSinceEpoch;
    if (_lastMill == null) {
      _lastMill = currentMill;
      return true;
    }

    int interval = currentMill - (_lastMill ?? 0);
    if (interval >= (minClickIntervalMill ?? 400)) {
      _lastMill = currentMill;
      return true;
    }
    return false;
  }
}

enum ClickType {
  // 抬起触发动画
  upTrigger,
  // 默认
  downTrigger,
}
